package cc.wanforme.st.server.base.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cc.wanforme.st.server.base.entity.RolePermission;
import cc.wanforme.st.server.base.service.IRolePermissionService;
import cc.wanforme.st.server.exception.MunkRuntimeException;
import cc.wanforme.st.server.vo.ResMessage;
import cc.wanforme.st.server.vo.SearchQueryVo;
import cc.wanforme.st.server.vo.base.RolePermissionVo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanne
 * @since 2022-03-03
 */
@Controller
@RequestMapping("/base/role-permission")
public class RolePermissionController {

	@Autowired
	private IRolePermissionService rolePermissionService;
	
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('rolePermission:add')")
	@ResponseBody
	public ResMessage<Void> add(@RequestBody RolePermission vo) {
		rolePermissionService.saveVo(vo);
		rolePermissionService.clearCache(vo.getRole());
		return ResMessage.success();
	} 
	
	@PostMapping("/update")
	@PreAuthorize("hasAuthority('rolePermission:update')")
	@ResponseBody
	public ResMessage<Void> update(@RequestBody RolePermission vo) {
		rolePermissionService.updateVo(vo);
		rolePermissionService.clearCache(vo.getRole());
		return ResMessage.success();
	}
	
	@PostMapping("/updateRole")
	//@PreAuthorize("hasAuthority('rolePermission:updateRole')")
	@PreAuthorize("hasAuthority('rolePermission:update')")
	@ResponseBody
	public ResMessage<Void> update(@RequestBody RolePermissionVo vo) {
		try {
			rolePermissionService.updateRoleAllPermissions(vo);
			rolePermissionService.clearCache(vo.getRole());
			return ResMessage.success();
		} catch (Exception e) {
			throw new MunkRuntimeException("角色权限保存失败！");
		}
	}
	
	
	@PostMapping("/del/{id}")
	@PreAuthorize("hasAuthority('rolePermission:delete')")
	@ResponseBody
	public ResMessage<Void> del(@PathVariable Long id ) {
		RolePermission po = rolePermissionService.getById(id);
		rolePermissionService.deleteVo(id);
		if(po!=null) {
			rolePermissionService.clearCache(po.getRole());
		}
		return ResMessage.success();
	}
	
	@PostMapping("/del")
	@PreAuthorize("hasAuthority('rolePermission:delete')")
	@ResponseBody
	public ResMessage<Void> del(@RequestBody RolePermission vo) {
		if(vo.getId() != null) {
			rolePermissionService.deleteVo(vo.getId());
		} else {
			rolePermissionService.deleteVo(vo.getRole(), vo.getPermission());
			rolePermissionService.clearCache(vo.getRole());
		}
		return ResMessage.success();
	}
	
	/** 角色模糊搜索
	 * @param queryVo
	 * @return
	 */
	@PostMapping("/list")
	@PreAuthorize("hasAuthority('rolePermission:list')")
	@ResponseBody
	public ResMessage<PageInfo<RolePermission>> list(@RequestBody SearchQueryVo<String> queryVo) {
		PageInfo<RolePermission> result = rolePermissionService.listRolePermissions(queryVo.getPage(),
				queryVo.getSize(), queryVo.getParam());
		return ResMessage.success(result);
	}
	
	/** 清除角色缓存
	 * @param queryVo
	 * @return
	 */
	@GetMapping("/clear")
	@PreAuthorize("hasAuthority('rolePermission:update')")
	@ResponseBody
	public ResMessage<Void> clear(String role) {
		rolePermissionService.clearCache(role);
		return ResMessage.success();
	}
	
	/** 获取角色的所有权限
	 * @param role
	 * @return
	 */
	@PostMapping("/role")
	@ResponseBody
	public ResMessage<List<RolePermission>> roleAllPermission(@RequestBody RolePermissionVo vo ) {
		List<RolePermission> list = rolePermissionService.selectPermissions(vo.getRole());
		return ResMessage.success(list);
	}
	
	@GetMapping("/roleTest")
	@ResponseBody
	public ResMessage<List<RolePermission>> roleAllPermission() {
		List<RolePermission> list = rolePermissionService.selectPermissions("tester");
		return ResMessage.success(list);
	}
	
}
